/**
 * 
 */
package nl.samenloop.bigscreen.fx;

import javax.swing.JComponent;

/**
 * @author Bram Wiekens
 *
 */
public class FadeIn {

	
	
	private FadeIn(){ /* Utility class doesn't have public constructor */ }
	
	
	public static void start(final JComponent component, int duration, final FxListener listener) {
		if (component instanceof Transparency) {
		
		final Transparency target = (Transparency) component;
			
		final float step = FxUtil.calcStepSize(duration);
			
		component.setIgnoreRepaint(true);
		component.setDoubleBuffered(true);
		
		Thread fadeIn = new Thread(new Runnable() {
			@Override
			public void run() {
				component.setVisible(false);
				target.setOpacity(0.0f);
				component.repaint();
				component.setVisible(true);
				
				while (target.getOpacity() < 1.0f) {
					target.setOpacity(target.getOpacity() + step);
					if (target.getOpacity() > 1.0f) {
						target.setOpacity(1.0f);
					}

					component.repaint();

					FxUtil.sleep();
				}

				if (listener != null) {
					listener.finished();
				}
			}
		});

		fadeIn.start();
		} else {
			throw new IllegalStateException("Cannot set Transparency on object");
		}
	}
	
	public static void start(final JComponent component, int duration){
		start(component, duration, null);
	}
	
}
